<template>
  <div class="xtx-carousel" @mouseenter="stopInter" @mouseleave="startInter">
    <!-- 图片 -->
    <ul class="carousel-body">
      <li
        v-for="(item, index) in sliders"
        :key="item.id"
        class="carousel-item "
        :class="{ fade: activeIndex === index }"
      >
        <RouterLink :to="item.hrefUrl">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <!-- 上一张 -->
    <a @click="prev" href="javascript:;" class="carousel-btn prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <!-- 下一张 -->
    <a @click="next" href="javascript:;" class="carousel-btn next"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <!-- 轮播图 示意点 -->
    <div class="carousel-indicator">
      <!-- 数组有几个 就生成几个点  基于数字的循环 i是从1开始的-->
      <span
        v-for="i in sliders.length"
        :key="i"
        :class="{ active: i - 1 === activeIndex }"
      ></span>
    </div>
  </div>
</template>

<script>
import { ref, onUnmounted } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 轮播图数据
    sliders: Array,
    autoplay: {
      type: Boolean,
      default: true
    },
    duration: {
      type: Number,
      default: 2000
    }
  },
  // 参数1:props 就是props的值
  // props是一个对象 包含了 props中的所有数据
  setup (props) {
    // 下标 索引
    const activeIndex = ref(0)
    // 定时器 id
    let id
    // 开启定时器
    const startInter = () => {
      if (props.autoplay === true) {
        // 开启轮播
        id = setInterval(() => {
          // 基本类型数据的修改 要通过.value
          activeIndex.value++
          if (activeIndex.value > props.sliders.length - 1) {
            activeIndex.value = 0
          }
          // console.log('轮播中!!')
        }, props.duration)
      }
    }
    // 关闭定时器
    const stopInter = () => {
      clearInterval(id)
    }
    // 下一张
    const next = () => {
      activeIndex.value++
      if (activeIndex.value > props.sliders.length - 1) {
        // 去第一张
        activeIndex.value = 0
      }
    }
    // 上一张
    const prev = () => {
      activeIndex.value--
      if (activeIndex.value < 0) {
        // 去最后一张
        activeIndex.value = props.sliders.length - 1
      }
    }

    // 销毁的 钩子函数 等同于 2.x中的 destoryed
    onUnmounted(() => {
      // console.log('onUnmounted')
      stopInter()
    })

    // 默认 开启一次定时器
    startInter()
    return {
      activeIndex,
      startInter,
      stopInter,
      next,
      prev
    }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel {
  width: 1240px;
  height: 500px;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 98;

  // 小点 错开左侧分类
  .carousel-indicator {
    padding-left: 250px;
  }
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
  // 箭头 错开左侧分类 保证权重
  .carousel-btn.prev {
    left: 270px;
  }
}
</style>
